package com.sec.android.app.myfiles.util;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.AppConstants;

import java.util.HashMap;

/**
 * Created by daelight.kim on 2016-10-17.
 */

public class FolderAppIconMgr {
    private static HashMap<String, FolderAppIconInfo> sFeaturedAppList = new HashMap<>();
    private static HashMap<String, Drawable> sMergedIconDrawable = new HashMap<>();

    private FolderAppIconMgr() {
    }

    public static class FolderAppIconInfo {
        public String mAppPkgName;
        public String mPath;
        public int mResId;

        FolderAppIconInfo(String path, String packageName, int resId) {
            mAppPkgName = packageName;
            mPath = path;
            mResId = resId;
        }

        public Drawable getMergedIcon(Context context, BitmapDrawable baseDrawable) {
            Drawable ret = sMergedIconDrawable.get(mPath);
            if (ret == null) {
                Bitmap orgBmp = baseDrawable.getBitmap();
                int fileIconHeight = orgBmp.getHeight();
                int fileIconWidth = (int) (fileIconHeight * (38.5 / 36));
                Bitmap targetBmp = Bitmap.createBitmap(fileIconWidth, fileIconHeight, Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(targetBmp);
                canvas.drawBitmap(orgBmp, 0, 0, null);

                Bitmap icon = null;
                Paint paint = null;

                if (mAppPkgName != null) {
                    try {
                        Drawable drawable = context.getPackageManager().getApplicationIcon(mAppPkgName);
                        icon = DrawableMgr.getBitmapFromDrawable(drawable);
                    } catch (PackageManager.NameNotFoundException e) {
                        e.printStackTrace();
                        return null;
                    }
                } else {
                    Drawable drawable = (Drawable) context.getResources().getDrawable(mResId, null);
                    if (drawable != null) {
                        //TODO HYL  Drawable 转 bitmap
                        //icon = drawable.getBitmap();
                    }
                    paint = new Paint();
                    paint.setColorFilter(new PorterDuffColorFilter(context.getResources().getColor(R.color.myfiles_list_folder_icon_tint, null), PorterDuff.Mode.SRC_IN));
                }

                int iconSize = getFolderAppIconSize(orgBmp);
                float LeftPoint = getFolderAppIconLeftPosition(context, orgBmp);
                float topPoint = getFolderAppIconTopPosition(orgBmp);

                if (icon != null) {
                    icon = Bitmap.createScaledBitmap(icon, iconSize, iconSize, true);
                    canvas.drawBitmap(icon, LeftPoint, topPoint, paint);
                    canvas.save();
                }
                ret = new BitmapDrawable(context.getResources(), targetBmp);
                sMergedIconDrawable.put(mPath, ret);
            }
            return ret;
        }

        public Drawable getMergedIconForTablet(Context context, BitmapDrawable baseDrawable) {
            Drawable ret = sMergedIconDrawable.get(mPath);
            if (ret == null) {
                Bitmap orgBmp = baseDrawable.getBitmap();
                Bitmap targetBmp = Bitmap.createBitmap(orgBmp.getWidth(), orgBmp.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(targetBmp);
                canvas.drawBitmap(orgBmp, 0, 0, null);

                Bitmap icon = null;
                float point = 0.0f;

                Drawable drawable = (Drawable) context.getResources().getDrawable(mResId, null);
                if (drawable != null) {
                    //TODO HYL  Drawable 转 bitmap
                    //icon = drawable.getBitmap();
                    point = getGivenIconPoint(orgBmp, icon);
                }

                if (icon != null) {
                    canvas.drawBitmap(icon, point, point, null);
                    canvas.save();
                }
                ret = new BitmapDrawable(context.getResources(), targetBmp);
                sMergedIconDrawable.put(mPath, ret);
            }
            return ret;
        }
    }

    private static float sGivenIconPoint = -1.0f;

    private static float getGivenIconPoint(Bitmap orgBmp, Bitmap target) {
        if (sGivenIconPoint < 0) {
            sGivenIconPoint = (orgBmp.getWidth() - target.getWidth()) / 1.5f;
        }
        return sGivenIconPoint;
    }

    private static float getFolderAppIconLeftPosition(Context context, Bitmap orgBmp) {
        return orgBmp.getWidth() - getFolderAppIconSize(orgBmp) + DrawableMgr.getEnlargeSize(context);
    }

    private static float getFolderAppIconTopPosition(Bitmap orgBmp) {
        return orgBmp.getHeight() - getFolderAppIconSize(orgBmp);
    }

    private static int getFolderAppIconSize(Bitmap orgBmp) {
        return orgBmp.getWidth() * 16 / 36;
    }

    private static void addApplication(String path, String packageName, int resId) {
        path = AppConstants.StoragePath.INTERNAL_ROOT + path;
        FolderAppIconInfo item = new FolderAppIconInfo(path, packageName, resId);
        sFeaturedAppList.put(path, item);
    }

    private static void addPrivateFolder(String path, int resId) {
        FolderAppIconInfo item = new FolderAppIconInfo(path, null, resId);
        sFeaturedAppList.put(path, item);
    }

    private static void addApplication(String path, String packageName) {
        addApplication(path, packageName, 0);
    }

    private static void addApplication(String path, int resId) {
        addApplication(path, null, resId);
    }

    static {
        /**
         * Android given folder
         */
        addApplication("/DCIM", R.drawable.myfiles_list_folder_dcim); //DCIM
        addApplication("/Download", R.drawable.myfiles_list_folder_downloads); //Download
        addApplication("/Music", R.drawable.myfiles_list_folder_music); //Music
        addApplication("/Movies", R.drawable.myfiles_list_folder_movies); //Movies
        addApplication("/Pictures", R.drawable.myfiles_list_folder_pictures); //Pictures
        addApplication("/Android", R.drawable.myfiles_list_folder_settings); //Android

        /**
         * App icon folder
         */

        //Google
        addApplication("/Pictures/Hangouts", "com.google.android.talk"); //Hangouts
        addApplication("/DCIM/Google Photos", "com.google.android.apps.photos"); //Photos
        addApplication("/DCIM/Auto", "com.google.android.apps.photos"); //Photos

        //3rd Party
        addApplication("/DCIM/Facebook", "com.facebook.katana"); //Facebook
        addApplication("/Pictures/Facebook", "com.facebook.katana"); //Facebook
        addApplication("/com.facebook.orca", "com.facebook.katana"); //Facebook
        addApplication("/Pictures/Messenger", "com.facebook.prca"); //Facebook

        addApplication("/WhatsApp", "com.whatsapp"); //Whatsapp

        addApplication("/Pictures/Instagram", "com.instagram.android"); //Instagram
        addApplication("/Movies/Instagram", "com.instagram.android"); //Instagram

        addApplication("/Snapchat", "com.snapchat.android"); //Snapchat

        addApplication("/KakaoTalk", "com.kakao.talk"); //KaKaoTalk
        addApplication("/Music/KakaoTalk", "com.kakao.talk"); //KaKaoTalk
        addApplication("/Pictures/KakaoTalk", "com.kakao.talk"); //KaKaoTalk

        addApplication("/Naver", "com.nhn.android.search"); //NAVER

        addApplication("/Tencent", "com.tencent.mm"); //WeChat
        addApplication("/Tencent/MicroMsg", "com.tencent.mm"); //WeChat

        addApplication("/tbslog", "com.tencent.mobileqq"); //QQ
        addApplication("/tencent/QQ_Images", "com.tencent.mobileqq"); //QQ
        addApplication("/tencent/QQfile_recv", "com.tencent.mobileqq"); //QQ
        addApplication("/tencent/QQ_Favorite", "com.tencent.mobileqq"); //QQ
        addApplication("/tencent/video_edit_music", "com.tencent.mobileqq"); //QQ

        addApplication("/alipay/multimedia/cache/image", "com.eg.android.AlipayGphone"); //Allpay Wallet
        addApplication("/alipay/multimedia/alipay_audio_files", "com.eg.android.AlipayGphone"); //Allpay Wallet

        addApplication("/Picture/taobao", "com.taobao.htao.android"); //Taobao Shopping

        addPrivateFolder("/storage/Private", R.drawable.myfiles_list_folder_private);
    }

    public static FolderAppIconInfo getApplicationItem(String path) {
        return sFeaturedAppList.get(path);
    }

    public static void clearMergedIconDrawables() {
        sMergedIconDrawable.clear();
    }
}
